package View;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Shows a pair of dice that are rolled when the user clicks a button
 * that appears below the dice.
 */
public class DicePanelWithButton extends JPanel {

   private int rollResult;  // The values shown on the dice.
   private Timer timer;   // Used to animate rolling of the dice.
   private JButton rollButton;
   /**
    *  The constructor sets up the panel.  It creates the button and
    *  the drawing surface panel on which the dice are drawn and puts
    *  them into a BorderLayout.  It adds an ActionListener to the button
    *  that rolls the dice when the user clicks the button.
    */
    public DicePanelWithButton() {

		this.setLayout(new BorderLayout(4,4));
		this.setBackground(Color.black);  // Will show through the gap in the BorderLayout.
		this.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 128)));

		JPanel dicePanel = new JPanel() {
			public void paintComponent(Graphics g) {
				super.paintComponent(g);  // fill with background color.
				drawDie(g, rollResult, 20, 20); // Just draw the dice.
			}
		};
		dicePanel.setPreferredSize( new Dimension(100,100) );
		dicePanel.setBackground( new Color(0, 0, 128) );  // blue
		add(dicePanel, BorderLayout.CENTER);               
		rollButton = new JButton("ROLL!"); rollButton.setBackground(Color.yellow); rollButton.setFont(new Font("Dialog", Font.PLAIN, 20));
		rollButton.setPreferredSize(new Dimension(50,70));
                rollButton.addActionListener( new ActionListener() 
                {
			public void actionPerformed(ActionEvent evt) 
                        {
				roll();
			}
		}
                );
                
		add(rollButton, BorderLayout.NORTH);
    } // end constructor

   /**
    * Draw a die.
    * The value parameter gives the
    * value showing on the die (that is, the number of dots).
    */
    void drawDie(Graphics g, int val, int x, int y) {
		g.setColor(Color.black);
		g.fillRect(x, y, 100, 100);
		g.setColor(Color.white);
		g.drawRect(x, y, 100, 100);
		if (val > 1){
			g.fillOval(x+10, y+10, 20, 20); // upper left dot
			g.fillOval(x+70, y+70, 20, 20); // bottom right dot
		}
		if (val > 3){
			g.fillOval(x+70, y+10, 20, 20); // upper right dot
			g.fillOval(x+10, y+70, 20, 20); // bottom left dot
		}
		if (val == 6){
			g.fillOval(x+10, y+40, 20, 20); // middle left dot
			g.fillOval(x+70, y+40, 20, 20); // middle right dot
		}
		if (val % 2 == 1){
			g.fillOval(x+40, y+40, 20, 20);// middle dot (for odd-numbered val's)
		}
    }

   /**
    * Run an animation that randomly changes the values shown on
    * the dice 15 times, every 100 milliseconds.
    */
    public void roll() {
		if (timer != null)
        return;
		timer = new Timer(100, new ActionListener() 
                {
			int frames;
			public void actionPerformed(ActionEvent evt) 
                        {
				rollResult = (int)(Math.random()*6) + 1;
                                
				repaint();
				frames++;
				if (frames == 15) 
                                {
					timer.stop();
					timer = null;
				}
			}
		});
		timer.start();
 
                
    }
} // end class DicePanelWithButton
